<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>线程同步与并发控制 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background-color: #f8f9fa;
            color: #333;
            line-height: 1.6;
        }
        h1, h2, h3, h4, h5, h6 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
            margin-bottom: 1rem;
        }
        .hero {
            background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
            color: white;
            position: relative;
            overflow: hidden;
        }
        .hero-content {
            position: relative;
            z-index: 2;
        }
        .hero::after {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: url('') repeat;
            opacity: 0.3;
        }
        .feature-card {
            transition: all 0.3s ease;
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
        }
        .feature-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .feature-icon {
            font-size: 2.5rem;
            margin-bottom: 1rem;
            color: #4f46e5;
        }
        .mermaid-container {
            background: white;
            border-radius: 12px;
            padding: 2rem;
            box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
            margin: 2rem 0;
        }
        .code-block {
            font-family: 'Courier New', Courier, monospace;
            background-color: #2d3748;
            color: #f8f9fa;
            border-radius: 8px;
            padding: 1.5rem;
            overflow-x: auto;
        }
        .drop-cap::first-letter {
            float: left;
            font-size: 4rem;
            line-height: 0.8;
            margin-right: 0.5rem;
            color: #4f46e5;
            font-weight: bold;
        }
        .footer {
            background-color: #1a202c;
            color: #f8f9fa;
        }
        .footer a {
            color: #a0aec0;
            transition: color 0.2s ease;
        }
        .footer a:hover {
            color: #fff;
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero py-20 px-4 md:py-32">
        <div class="container mx-auto max-w-6xl hero-content">
            <div class="text-center">
                <h1 class="text-4xl md:text-6xl font-bold mb-6">线程同步与并发控制</h1>
                <p class="text-xl md:text-2xl mb-8 opacity-90">深入理解多线程编程中的关键技术与最佳实践</p>
                <div class="flex justify-center space-x-4">
                    <a href="#intro" class="px-6 py-3 bg-white text-indigo-600 font-semibold rounded-lg hover:bg-opacity-90 transition">开始阅读</a>
                    <a href="#visualization" class="px-6 py-3 border-2 border-white text-white font-semibold rounded-lg hover:bg-white hover:bg-opacity-10 transition">查看图表</a>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-6xl px-4 py-12">
        <!-- Introduction -->
        <section id="intro" class="mb-16">
            <p class="drop-cap text-lg md:text-xl mb-8">在现代软件开发中，多线程编程已经成为提升程序性能的重要手段。然而，线程间的资源共享和同步问题也随之而来，成为开发者必须面对的挑战。线程同步机制正是为了解决这些问题而设计的一系列技术，它们确保在多线程环境下数据的正确性和程序的可预测性。</p>
            
            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-8 mb-16">
                <div class="feature-card p-8 bg-white">
                    <div class="feature-icon">
                        <i class="fas fa-lock"></i>
                    </div>
                    <h3 class="text-2xl font-semibold mb-4">互斥锁</h3>
                    <p>最基本的同步机制，保证同一时间只有一个线程可以访问共享资源，防止数据竞争。</p>
                </div>
                <div class="feature-card p-8 bg-white">
                    <div class="feature-icon">
                        <i class="fas fa-exchange-alt"></i>
                    </div>
                    <h3 class="text-2xl font-semibold mb-4">信号量</h3>
                    <p>控制对有限数量资源的访问，允许多个线程在限制条件下共享资源。</p>
                </div>
                <div class="feature-card p-8 bg-white">
                    <div class="feature-icon">
                        <i class="fas fa-bell"></i>
                    </div>
                    <h3 class="text-2xl font-semibold mb-4">条件变量</h3>
                    <p>使线程能够等待特定条件变为真，是构建复杂同步模式的基础。</p>
                </div>
            </div>
        </section>

        <!-- Visualization Section -->
        <section id="visualization" class="mb-16">
            <h2 class="text-3xl md:text-4xl font-bold mb-8 text-center">线程同步机制关系图</h2>
            <div class="mermaid-container">
                <div class="mermaid">
                    graph TD
                    A[线程同步] --> B[互斥锁 Mutex]
                    A --> C[读写锁 RWLock]
                    A --> D[信号量 Semaphore]
                    A --> E[条件变量 Condition Variable]
                    A --> F[屏障 Barrier]
                    A --> G[原子操作 Atomic]
                    
                    B --> H[保证独占访问]
                    C --> I[提高读多写少场景性能]
                    D --> J[控制资源访问数量]
                    E --> K[线程间通信与协调]
                    F --> L[同步多个线程进度]
                    G --> M[无锁编程基础]
                </div>
            </div>
        </section>

        <!-- Code Examples -->
        <section class="mb-16">
            <h2 class="text-3xl md:text-4xl font-bold mb-8">常见同步模式示例</h2>
            
            <div class="mb-12">
                <h3 class="text-2xl font-semibold mb-4">互斥锁使用示例</h3>
                <div class="code-block">
                    <pre><code>#include &lt;pthread.h&gt;

pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
int shared_counter = 0;

void* thread_function(void* arg) {
    pthread_mutex_lock(&lock);  // 获取锁
    shared_counter++;           // 临界区
    pthread_mutex_unlock(&lock);// 释放锁
    return NULL;
}</code></pre>
                </div>
            </div>
            
            <div class="mb-12">
                <h3 class="text-2xl font-semibold mb-4">生产者-消费者模式</h3>
                <div class="code-block">
                    <pre><code>#include &lt;semaphore.h&gt;

sem_t empty, full, mutex;
Buffer buffer;

void producer() {
    while (true) {
        item = produce_item();
        sem_wait(&empty);
        sem_wait(&mutex);
        buffer.insert(item);
        sem_post(&mutex);
        sem_post(&full);
    }
}

void consumer() {
    while (true) {
        sem_wait(&full);
        sem_wait(&mutex);
        item = buffer.remove();
        sem_post(&mutex);
        sem_post(&empty);
        consume_item(item);
    }
}</code></pre>
                </div>
            </div>
        </section>

        <!-- Best Practices -->
        <section class="mb-16">
            <h2 class="text-3xl md:text-4xl font-bold mb-8">线程同步最佳实践</h2>
            
            <div class="grid grid-cols-1 md:grid-cols-2 gap-8">
                <div class="bg-white p-8 rounded-lg shadow-md">
                    <h3 class="text-2xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-check-circle text-green-500 mr-3"></i>
                        设计原则
                    </h3>
                    <ul class="space-y-3">
                        <li class="flex items-start">
                            <i class="fas fa-chevron-right text-indigo-500 mt-1 mr-2 text-sm"></i>
                            <span>最小化临界区范围，减少锁持有时间</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-chevron-right text-indigo-500 mt-1 mr-2 text-sm"></i>
                            <span>避免嵌套锁，防止死锁发生</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-chevron-right text-indigo-500 mt-1 mr-2 text-sm"></i>
                            <span>优先使用高级同步抽象而非低级别原语</span>
                        </li>
                    </ul>
                </div>
                
                <div class="bg-white p-8 rounded-lg shadow-md">
                    <h3 class="text-2xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-exclamation-triangle text-yellow-500 mr-3"></i>
                        常见陷阱
                    </h3>
                    <ul class="space-y-3">
                        <li class="flex items-start">
                            <i class="fas fa-times-circle text-red-500 mt-1 mr-2 text-sm"></i>
                            <span>忘记释放锁导致死锁或资源泄露</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-times-circle text-red-500 mt-1 mr-2 text-sm"></i>
                            <span>锁粒度过大造成性能瓶颈</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-times-circle text-red-500 mt-1 mr-2 text-sm"></i>
                            <span>错误使用条件变量导致虚假唤醒</span>
                        </li>
                    </ul>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="footer py-8">
        <div class="container mx-auto max-w-6xl px-4 text-center">
            <p class="text-sm md:text-base">
                <span>技术小馆</span>
                <span class="mx-2">|</span>
                <a href="http://www.yuque.com/jtostring" target="_blank" rel="noopener">http://www.yuque.com/jtostring</a>
            </p>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>